package com.heihei.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heihei.beans.LoadDatas;
import com.heihei.beans.PageBean;
import com.heihei.domain.Websitesinfo;
import com.heihei.mapper.WebsitesinfoMapper;
import com.heihei.service.WebsitesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WebsitesServiceImpl implements WebsitesService {
    @Autowired
    private WebsitesinfoMapper websitesinfoMapper;

    //查询所有网址信息
    /*@Override
    public List<Websitesinfo> selectAllSites() {
        return websitesinfoMapper.selectByExample(null);
    }*/

    @Override
    public LoadDatas findAllConditionally(LoadDatas datas) {
        //开始分页
        PageHelper pageHelper = new PageHelper();

        PageBean pb = datas.getPb();

        pageHelper.startPage(datas.getPb().getCurrentPage(), datas.getPb().getPageSize());
        //pageHelper.startPage(pb.getCurrentPage(), 2);

        //查询数据
        List<Websitesinfo> websitesinfos = websitesinfoMapper.findAllConditionally(datas);

//        System.out.println(websitesinfos);

        PageInfo<Websitesinfo> pageInfo = new PageInfo<Websitesinfo>(websitesinfos);
        //将总条数和总页数加入到datas

        pb.setTotalCount(pageInfo.getTotal());
        pb.setTotalPage(pageInfo.getPages());
        pb.setCurrentPage(pageInfo.getPageNum());
        pb.setPageSize(pageInfo.getPageSize());

//        System.out.println(pageInfo.getTotal());
//        System.out.println(pageInfo.getPages());

        //将查询出来的数据加入datas
        datas.setSites(websitesinfos);

//        System.out.println(websitesinfos);

//        System.out.println(datas.getPb());

        return datas;
    }

    //添加数据
    public void insertWebsiteInfo(Websitesinfo websitesinfo){
        //设置日期
        websitesinfo.setEditTime(new Date());
        //websitesinfo.setUserid(1);

        websitesinfoMapper.insert(websitesinfo);
    }

    //修改数据
    @Override
    public void updateWebsiteInfo(Websitesinfo websitesinfo) {
        websitesinfoMapper.updateByPrimaryKeySelective(websitesinfo);
    }

    //根据id值回显数据
    @Override
    public Websitesinfo getWebsiteInfoById(Integer id) {
        return websitesinfoMapper.selectByPrimaryKey(id);
    }

    //根据id删除
    @Override
    public void deleteWebsiteInfoById(Integer id) {
        websitesinfoMapper.deleteByPrimaryKey(id);
    }


}
